﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace libSyllabus.Model
{
    /// <summary>
    /// This is Class entity class, which represents an entry in the Classes table.
    /// </summary>
    /// <remarks>
    /// For now, Classes table contains following columns:
    ///  - ID                : Auto Generated Index (Number)
    ///  - SectionID      : ID of the parent Section (Number)
    ///  - CourseID       : ID of the parent Course (Number)
    ///  - Title             : Title of the class, probably main topic (String)
    ///  - Description    : Description of the topic (String)
    ///  - Time            : Date and Time of the class start (DateTime)
    ///  - Duration       : Duration of class in minutes (DateTime)
    ///  - IsCancelled     : Whether the class has been cancelled (Boolean)
    ///  
    /// Note: There will be a class entry for EVERY single class
    ///  
    /// Property Change Notification is taken care of in the base class
    /// </remarks>
    [Table(Name = "Classes")]
    public sealed class ClassEntity : EntityBase
    {
        #region Constants

        //Column Name Constants
        public const string CLASS_ID = "ID";
        public const string CLASS_SECTIONID = "SectionID";
        public const string CLASS_COURSEID = "CourseID";
        private const string CLASS_NAME = "Title";
        private const string CLASS_DESCRIPTION = "Description";
        private const string CLASS_TIME = "Time";
        private const string CLASS_DURATION = "Duration";
        private const string CLASS_ISCANCELLED = "IsCanceled";

        #endregion

        #region Constructors

        public ClassEntity()
        {
            m_course = new EntityRef<CourseEntity>();
            m_section = new EntityRef<SectionEntity>();
        }

        #endregion

        #region Fields

        //Fields mapped to the database
        [Column(Name = CLASS_ID, IsPrimaryKey = true, CanBeNull = false, IsDbGenerated = true,
            DbType = "INT NOT NULL Identity", AutoSync = AutoSync.OnInsert)]
        private Int32 m_id;

        [Column(Name = CLASS_SECTIONID, CanBeNull = false)]
        private Int32 m_sectionID;

        [Column(Name = CLASS_COURSEID, CanBeNull = false)]
        private Int32 m_courseID;

        [Column(Name = CLASS_NAME, CanBeNull = false)]
        private String m_title;

        // NULL means empty, equivalent to "" or String.Empty
        [Column(Name = CLASS_DESCRIPTION, CanBeNull = true)]
        private String m_description;

        [Column(Name = CLASS_TIME, CanBeNull = false)]
        private DateTime m_time;

        [Column(Name = CLASS_DURATION, CanBeNull = false)]
        private Int32 m_duration;

        [Column(Name = CLASS_ISCANCELLED, CanBeNull = false)]
        private Boolean m_isCancelled;


        // Mapped fields

        private EntityRef<SectionEntity> m_section;

        private EntityRef<CourseEntity> m_course;

        #endregion

        #region Properties

        public Int32 ID
        {
            get
            {
                return m_id;
            }
        }

        public Int32 SectionID
        {
            get
            {
                return m_sectionID;
            }
        }

        public Int32 CourseID
        {
            get
            {
                return m_courseID;
            }
        }

        public String Title
        {
            get
            {
                return m_title;
            }
            set
            {
                if (m_title != value)
                {
                    RaisePropertyChanging(CLASS_NAME);
                    m_title = value;
                    RaisePropertyChanged(CLASS_NAME);
                }
            }
        }

        public String Description
        {
            get
            {
                return m_description == null ? String.Empty : m_description;
            }
            set
            {
                if (m_description != value)
                {
                    RaisePropertyChanging(CLASS_DESCRIPTION);
                    m_description = value;
                    RaisePropertyChanged(CLASS_DESCRIPTION);
                }
            }
        }

        public DateTime Time
        {
            get
            {
                return m_time;
            }
            set
            {
                if (m_time != value)
                {
                    RaisePropertyChanging(CLASS_TIME);
                    m_time = value;
                    RaisePropertyChanged(CLASS_TIME);
                }
            }
        }

        public Int32 Duration
        {
            get
            {
                return m_duration;
            }
            set
            {
                if (m_duration != value)
                {
                    RaisePropertyChanging(CLASS_DURATION);
                    m_duration = value;
                    RaisePropertyChanging(CLASS_DURATION);
                }
            }
        }

        public Boolean IsCancelled
        {
            get
            {
                return m_isCancelled;
            }
            set
            {
                if (m_isCancelled != value)
                {
                    RaisePropertyChanging(CLASS_ISCANCELLED);
                    m_isCancelled = value;
                    RaisePropertyChanged(CLASS_ISCANCELLED);
                }
            }
        }

        [Association(Storage = "m_course", ThisKey = CLASS_COURSEID)]
        public CourseEntity Course
        {
            get
            {
                return m_course.Entity;
            }
        }

        [Association(Storage = "m_section", ThisKey = CLASS_SECTIONID)]
        public SectionEntity Section
        {
            get
            {
                return m_section.Entity;
            }
        }

        #endregion
    }
}
